package com.tcsl.exercise.demo.handler;

import com.tcsl.exercise.demo.entity.R;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public R defaultExceptionHandler(HttpServletRequest request, BindException  e){
        e.printStackTrace();
        return R.err().setMsg(e.getBindingResult().getFieldError().getDefaultMessage());
    }
}
